let mixinList = require('../../common/mixinList');
Page({
    behaviors: [mixinList],
    data: {
        navInfo: '',
        supplier_id: '', //接收到的供应商id
        detailLoading: true,
        info: null, //供应商信息
        tab_active: 0,
        goodsInfo: {
            cate_list: [], //商品分类列表
            active: 0,
        },
        usePrivateMethod: true,
        showAddCartPopup: false, //展示加入购物车弹窗
        addCartCurrent: null, //加入购物车商品信息
        evaluateScreenActive: 0, //评价筛选选中项
        evaluateScreenData: [
            { type: '全部', num: 0 },
            { type: '好评', num: 0 },
            { type: '中评', num: 0 },
            { type: '差评', num: 0 },
        ],
    },
    onLoad: function (options) {
        this.setData({ navInfo: getApp().globalData.navInfo });
        const { id, uid, index } = options;
        if (id) this.data.supplier_id = id;
        if (uid) {
            //绑定关系
            return this.redirect_login(uid);
        }
        if (index != undefined) this.data.lastPageIndex = index;
        this.getDetail();
    },
    /**获取供应商详情 */
    getDetail() {
        this.request(
            {
                url: '/api/supplier/detail',
                login: false,
                data: { supplier_id: this.data.supplier_id },
            },
            res => {
                let cate_list = res.data.cate_list;
                delete res.data.cate_list;

                this.setData({
                    info: res.data,
                    ['goodsInfo.cate_list']: cate_list,
                    detailLoading: false,
                });
                cate_list.length > 0 && this.getList(true);
                this.getEvaluateNum();
            }
        );
    },
    getReqUrl() {
        const { tab_active, supplier_id, evaluateScreenActive } = this.data;
        if (tab_active === 1) {
            return `/api/comment?supplier_id=${supplier_id}&type=${evaluateScreenActive}`;
        }
        return '/api/product/index';
    },
    getParamsData() {
        //页面特殊的请求参数，需要时在页面内覆盖此方法，默认返回{}
        const { supplier_id, tab_active, goodsInfo } = this.data;

        if (tab_active === 1) return;
        return {
            supplier_id,
            cate_id: goodsInfo.cate_list[goodsInfo.active].cate_id,
        };
    },
    privateMethod(refresh, res) {
        return new Promise(resolve => {
            if (this.data.tab_active === 1) {
                this.data.evaluateScreenData[0].num ||
                    this.setData({
                        ['evaluateScreenData[0].num']: res.count,
                    });
                resolve(res.data);
                return;
            }
            resolve(res.data);
        });
    },
    onTabChange(e) {
        const { index } = e.detail;
        this.setData({ tab_active: index });
        if (index > 1) return;
        if (index === 0) {
            if (this.data.goodsInfo.cate_list.length === 0)
                return this.setData({ list: [], empty: true });
        }
        this.getList(true);
    },
    /**分类切换 */
    onClassChange({ detail }) {
        this.setData({
            ['goodsInfo.active']: detail,
        });
        this.getList(true);
    },
    //前往商品详情
    toDetail(e) {
        const index = e.detail.index;
        wx.navigateTo({
            url: `/pages/good_detail/index?id=${
                this.data.tabs[this.data.tab_active].data[index].product_id
            }`,
            events: {
                likeOption: () => {
                    //点击前往详情，详情页收藏、取消收藏操作成功时，此页该条数据同步
                    const { tabs, tab_active } = this.data;
                    const collected = tabs[tab_active].data[index].collected;
                    this.setData({
                        ['tabs[' +
                        tab_active +
                        '].data[' +
                        index +
                        '].collected']: collected === 1 ? 0 : 1,
                    });
                },
            },
        });
    },
    /**点击加入购物车 */
    openAddcartPopup(e) {
        const { info } = e.currentTarget.dataset;

        if (info.sku === 0)
            return wx.showToast({
                title: '该商品已售空',
                icon: 'none',
            });
        const { imgHost } = this.data;
        const addCartCurrent = {
            product_img: imgHost + info.product_img,
            product_name: info.product_name,
            product_price: info.product_price,
            sku: info.sku,
            unit: info.unit,
            supplier_id: info.supplier.supplier_id,
            supplier_name: info.supplier.title,
            level: info.level,
            norm: info.norm,
            product_id: info.product_id,
            sell_num: info.sell_num,
        };

        this.setData({
            addCartCurrent,
            showAddCartPopup: true,
        });
    },
    /**评价筛选条件改变 */
    onScreenChange(e) {
        const { index } = e.currentTarget.dataset;
        if (index === this.data.evaluateScreenActive) return;
        this.setData({ evaluateScreenActive: index });
        this.getList(true);
    },
    /**获取评价数量 */
    getEvaluateNum() {
        const url = `/api/comment/tongji?supplier_id=${this.data.supplier_id}`;
        this.request(
            {
                url,
            },
            ({ data }) => {
                this.setData({
                    ['evaluateScreenData[1].num']: data.praise_count,
                    ['evaluateScreenData[2].num']: data.medium_count,
                    ['evaluateScreenData[3].num']: data.negative_count,
                });
            }
        );
    },
    /**点击收藏 */
    collect() {
        this.request(
            {
                url: '/api/user/collect/collect',
                method: 'POST',
                data: {
                    status: this.data.info.collected === 0 ? '1' : '0',
                    type: 2,
                    can_id: this.data.supplier_id,
                },
            },
            res => {
                wx.showToast({
                    title:
                        this.data.info.collected === 0
                            ? '收藏成功'
                            : '取消成功',
                    icon: 'success',
                });
                this.setData({
                    ['info.collected']: this.data.info.collected === 0 ? 1 : 0,
                });
            },
            () => {
                wx.showToast({
                    title:
                        this.data.info.collected === 0
                            ? '收藏失败'
                            : '取消失败',
                    icon: 'error',
                });
            }
        );
    },
});
